package com.mine.site.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.mine.site.model.bsUser;
import com.mine.site.util.DbUtil;


public class bsUserDao {
	public List<bsUser> findAll() {
		List<bsUser> users = new ArrayList<bsUser>();
		String sql = "select * from bs_user";
		Statement stmt = null;
		try {
			stmt = DbUtil.getInstance().getConn().createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				bsUser u = new bsUser();
				u.setLoginId(rs.getString(1));
				u.setName(rs.getString(2));
				u.setLoginPwd(rs.getString(3));
				users.add(u);
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return users;
	}
	
	public void save(bsUser u){
		String sql="insert into bs_user values(?,?,?)";
		PreparedStatement pst = null;
		try {
			pst = DbUtil.getInstance().getConn().prepareStatement(sql);
			pst.setString(1, u.getLoginId());
			pst.setString(2, u.getName());
			pst.setString(3, u.getLoginPwd());
			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				pst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public void delete(String loginId){
		String sql = "delete from bs_user where id=?";
		PreparedStatement pst = null;
		try {
			pst = DbUtil.getInstance().getConn().prepareStatement(sql);
			pst.setString(1, loginId);
			
			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				pst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
 public bsUser findById(String loginId){
		 
		 String sql = "select * from bs_user where id=?";
			bsUser u =null;
			try {
				PreparedStatement pst = DbUtil.getInstance().getConn()
						.prepareStatement(sql);
				pst.setString(1, loginId);
				ResultSet rs = pst.executeQuery();

				
				if (rs.next()) {
					u = new bsUser();
					u.setLoginId(rs.getString(1));
					u.setName(rs.getString(2));
					u.setLoginPwd(rs.getString(3));
				}
				rs.close();
				pst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return u;
	 }
 public void update(bsUser u){
		String sql_update = "update bs_user set name=?,password=? where id=?";
		PreparedStatement pst = null;
		try {
			pst = DbUtil.getInstance().getConn().prepareStatement(sql_update);
			pst.setString(1, u.getName());
			pst.setString(2, u.getLoginPwd());
			pst.setString(3, u.getLoginId());
			
			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				pst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
 
}
